﻿namespace Digitalmes.Domain.Mes;

/// <summary>
/// 产品返工单。
/// </summary>
/// <remarks>在产品返工结束之后记录。</remarks>
[SugarTable("rework_doc", "产品返工表")]
[SugarIndex("index_rework_doc_sn", nameof(Sn), OrderByType.Asc)]
public class ReworkDoc : EntityBase
{
    /// <summary>
    /// 产品返工单据号
    /// </summary>
    [NotNull]
    public string? Doc { get; set; }

    /// <summary>
    /// SN
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "SN", Length = 64)]
    public string? Sn { get; set; }

    /// <summary>
    /// NG 的产线Id
    /// </summary>
    public long NgLineId { get; set; }

    /// <summary>
    /// NG 的产线编号
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "NG的产线编号", Length = 32)]
    public string? NgLineCode { get; set; }

    /// <summary>
    /// NG 的工位Id
    /// </summary>
    public long NgOpId { get; set; }

    /// <summary>
    /// NG 的工位编号
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "NG 的工位编号", Length = 32)]
    public string? NgOpCode { get; set; }

    /// <summary>
    /// NG 的工序Id
    /// </summary>
    public long NgProcId { get; set; }

    /// <summary>
    /// NG 的工序编号
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "NG 的工序编号", Length = 32)]
    public string? NgProcCode { get; set; }

    /// <summary>
    /// 当前所在的工艺路线 Id。没有则为 0。
    /// </summary>
    /// <remarks>线外站时设置为 0。</remarks>
    public long NgRoutingId { get; set; }

    /// <summary>
    /// 当前所在的工艺路线编号。
    /// </summary>
    /// <remarks>线外站产品，可设置为空。</remarks>
    [NotNull]
    [SugarColumn(ColumnDescription = "当前所在的工艺路线编号", Length = 32)]
    public string? NgRoutingCode { get; set; }

    /// <summary>
    /// NG 步序集合。
    /// </summary>
    /// <remarks></remarks>
    [SugarColumn(ColumnDescription = "NG步序号", IsJson = true, Length = 512)]
    public List<int>? NgSteps { get; set; }

    /// <summary>
    /// 返工类型（离线返工/在线返工）
    /// </summary>
    [SugarColumn(ColumnDescription = "返工类型")]
    public ReworkTypeEnum ReworkType { get; set; } = ReworkTypeEnum.Offline;

    /// <summary>
    /// 针对离线返工，返工后是否需要再重新上线。
    /// </summary>
    public bool NeedRelaunch { get; set; }

    /// <summary>
    /// 返工后重新上线的工序 Id。
    /// </summary>
    /// <remarks>
    /// 若是离线返工，表示返工完成后再重新上线开始作业的工序 Id；
    /// 若是在线返工，表示上线后开始返工作业的工序 Id。
    /// </remarks>
    public long RelaunchProcId { get; set; }

    /// <summary>
    /// 返工后重新上线的工序编号。
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "返工后重新上线的工序编号", Length = 32)]
    public string? RelaunchProcCode { get; set; }

    /// <summary>
    /// 返工后重新上线开始作业的步序编号。
    /// </summary>
    /// <remarks></remarks>
    [SugarColumn(ColumnDescription = "返工后重新上线开始作业的步序编号")]
    public int RelaunchProcStep { get; set; }

    /// <summary>
    /// 需要返工的工序集合，按工序顺序排列。
    /// </summary>
    /// <remarks>
    /// 根据重新上线工序按工艺路线计算（重上线工序到产品NG工序之间的所有工序），这些工序表示产品需要进行作业的工序（可能是重复作业）。
    /// </remarks>
    [NotNull]
    [SugarColumn(ColumnDescription = "需要返工的工序集合", IsJson = true, Length = 512)]
    public List<string>? ReworkProcCodes { get; set; }

    /// <summary>
    /// 返工模板Id
    /// </summary>
    public long? TemplateId { get; set; }

    /// <summary>
    /// 返工模板名称
    /// </summary>
    public string? TemplateName { get; set; }

    /// <summary>
    /// 返工单状态
    /// </summary>
    public ReworkStatusEnum Status { get; set; } = ReworkStatusEnum.Created;

    /// <summary>
    /// 返工人员
    /// </summary>
    [SugarColumn(ColumnDescription = "返工人员", Length = 64)]
    public string? Operator { get; set; }

    /// <summary>
    /// 返工完成时间
    /// </summary>
    public DateTime? CompletedTime { get; set; }

    /// <summary>
    /// 返修结果（OK/NG）
    /// </summary>
    /// <remarks>注：返工结果都为 OK</remarks>
    public ReworkResultEnum Result { get; set; } = ReworkResultEnum.OK;

    /// <summary>
    /// 针对离线返工，返工的产线编号
    /// </summary>
    public string? ReworkLineCode { get; set; }

    /// <summary>
    /// 针对离线返工，返工的工位编号
    /// </summary>
    public string? ReworkOpCode { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    [SugarColumn(ColumnDescription = "备注", Length = 512)]
    public string? Remark { get; set; }

    /// <summary>
    /// 返工模板明细项。
    /// </summary>
    /// <remarks>针对于离线返工。</remarks>
    [Navigate(NavigateType.OneToMany, nameof(ReworkDocLine.DocId))]
    public List<ReworkDocLine>? TemplateItems { get; set; }
}
